﻿/*
==============================================================================
// 用户信息管理 页面控制层代码。
==============================================================================
*/
(function ($) {

    if (!window.platform) { window.platform = new Object(); }
    if (!window.platform.UserManage) { window.platform.UserManage = new Object(); }

    window.platform.UserManage.initPage = function (createFunction, editFunction, removeFunction) {

        var _bindQueryButtonEvent = function () {
            $("#aquery", "#queryFm").bind("click", function () {
                var fm = $('#queryFm').serialize();
                $('#dg').datagrid('load', { model: fm });
            });
        };

        var _bindToolBarButtonEvent = function () {
            $("#acreate", "#toolbar").bind("click", function () {
                _createUser();
            });
            $("#aedit", "#toolbar").bind("click", function () {
                _editUser();
            });
            $("#aremove", "#toolbar").bind("click", function () {
                _batchAction("Remove");
            });

            $("#areload", "#toolbar").bind("click", function () {
                _reloadUser();
            });
            $("#asave", "#tb").bind("click", function () {

                _saveUser($('#dd'));

            });
            $("#aclose", "#tb").bind("click", function () {

                $('#dd').dialog('close');


            });
        };

        var _userDataGridInit = function () {
            var options = {
                singleSelect: true,
                checkOnSelect: false,
                selectOnCheck: false,
                fit: true,
                border: false,
                rownumbers: true,
                nowrap: true,
                striped: true,
                remoteSort: false,
                url: "/User/UserJson",
                queryParams: { model: $('#queryFm').serialize() },
                idField: "ID",
                frozenColumns: [[
                      { field: 'ID', checkbox: true },
                      { field: 'UserName', title: '用户名', width: 100, align: 'center', sortable: true },
                     { field: 'RealName', title: '姓名', width: 100, align: 'center', sortable: true }
                ]],
                columns: [[

                  
                   {
                       field: 'Sex', title: '性别', width: 100, align: 'center', sortable: true,
                       formatter: function (val, row) {
                           if (val) {
                               return "女";
                           }
                           else {
                               return "男";
                           }
                       }

                   },
                   { field: 'Tel', title: '电话', width: 100, align: 'center' },
                     { field: 'Mobile', title: '手机号码', width: 100, align: 'center' },
                   { field: 'Email', title: '邮箱', width: 150, align: 'center' },
                   { field: 'RoleName', title: '所屬角色', width: 150, align: 'center' }
                 
                ]],
                pagination: true,

                toolbar: "#toolbar",
                onDblClickRow: function (rowIndex, rowData) {
                    if (editFunction) {
                        _editUser(rowData);
                    }
                },
                onLoadSuccess: function (data) {
                    _resetBtnState();
                },
                onCheck: function (rowIndex, rowData) {
                    _resetBtnState();
                },
                onUncheck: function (rowIndex, rowData) {
                    _resetBtnState();
                },
                onCheckAll: function (rows) {
                    _resetBtnState();
                },
                onUncheckAll: function (rows) {
                    _resetBtnState();
                }
            };

            $("#dg").datagrid(options);
        };



        _bindQueryButtonEvent();
        _bindToolBarButtonEvent();
        _userDataGridInit();
    };

    _resetBtnState = function () {
        var rows = $('#dg').datagrid('getChecked');
        if (rows.length != 0) {
            if (rows.length > 1) {
                //若选中了多条，禁用编辑按钮
                $('#aedit').linkbutton('disable');
            }
            else {
                //若选中仅一条，开放编辑按钮
                $('#aedit').linkbutton('enable');
            }
            $('#aremove').linkbutton('enable');
            $('#areset').linkbutton('enable');
        }
        else {
            //全不选，禁用按钮
            $('#aedit').linkbutton('disable');
            $('#aremove').linkbutton('disable');
            $('#areset').linkbutton('disable');
        }
    };

    _createUser = function () {
        $('#dd').dialog({
            title: '新建用户',
            width: 690,
            height: 360,
            closed: false,
            cache: false,
            resizable: true,
            href: '/User/CreateEdit?id=0',
            modal: true,
            buttons: '#tb'


        });

    };

    _editUser = function (row) {
        if (row == undefined) {
            row = $('#dg').datagrid('getChecked')[0];
        }
        if (row) {
            $('#dd').dialog({
                title: '编辑用户',
                width: 690,
                height: 360,
                closed: false,
                cache: false,
                resizable: true,
                href: '/User/CreateEdit?id=' + row.ID,
                modal: true,
                buttons: '#tb'


            });
        }
    };

    _saveUser = function (dialog) {
        var verifyResult = $("form", dialog).form("validate");
        if (!verifyResult) { return false };




        var user = $(dialog).form().serializeObject();
        var _callback = function (result) {
            if (result.success) {
                //$.messager.alert('操作提醒', '操作成功');
                _reloadUser();
            } else {
                $.messager.alert('错误提醒', result.msg, 'error');
            }
            $('#dd').dialog('close');
        };

        $.post("/User/Save", user, function (result) {
            _callback.call(this, result);
        }, 'json');
    };

    _reloadUser = function () {
        $('#dg').datagrid('clearChecked');
        $('#dg').datagrid('clearSelections');
        $('#dg').datagrid('reload');
    };

    _batchAction = function () {

        var rows = $('#dg').datagrid('getChecked');
        var data = [];
        if (rows.length != 0) {
            for (var i = 0; i < rows.length; i++) {
                data.push(rows[i].ID);
            }
        }
        if (data.length != 0) {
            $.messager.confirm('确认', "您确定要删除所选用户吗", function (r) {
                if (r) {
                    $.post("/User/BatchDelete", { ids: data.join(',') }, function (result) {
                        if (result.success) {
                            //$.messager.alert('操作提醒', '操作成功');
                            _reloadUser();
                        } else {
                            $.messager.alert('错误提醒', result.msg, 'error');
                        }
                    }, "json");
                }
            });
        }
    };

    _singleAction = function (id) {

        $.messager.confirm('确认', "您确定要删除该用户吗", function (r) {
            if (r) {
                $.post("/User/SingleDelete", { id: id }, function (result) {
                    if (result.success) {
                        //$.messager.alert('操作提醒', '操作成功');
                        _reloadUser();
                    } else {
                        $.messager.alert('错误提醒', result.msg, 'error');
                    }
                }, "json");
            }
        });
    }


})(jQuery);